home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / QuickTime / JPEG Sample / Source / scrolls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-05  |  6.7 KB  |  306 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #        scrolls.c
  4. #
  5. #        This segment handles the scroll bars.
  6. #
  7. #        Author(s):     Michael Marinkovich & Guillermo Ortiz
  8. #                    marink@apple.com
  9. #
  10. #        Modification History: 
  11. #
  12. #            4/3/96        MWM     Initial coding                     
  13. #
  14. #        Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
  15. #
  16. #
  17. #        You may incorporate this sample code into your applications without
  18. #        restriction, though the sample code has been provided "AS IS" and the
  19. #        responsibility for its operation is 100% yours.  However, what you are
  20. #        not permitted to do is to redistribute the source as "DSC Sample Code"
  21. #        after having made changes. If you're going to re-distribute the source,
  22. #        we require that you make it clear in the source that the code was
  23. #        descended from Apple Sample Code, but that you've made changes.
  24. #
  25. *************************************************************************************/
  26.  
  27. #include <Events.h>
  28. #include <ToolUtils.h>
  29. #include <Gestalt.h>
  30. #include <OSUtils.h>
  31.  
  32.  
  33. #include "App.h"
  34. #include "Proto.h"
  35.  
  36.  
  37. //----------------------------------------------------------------------
  38. //
  39. //    Globals - 
  40. //
  41. //----------------------------------------------------------------------
  42.  
  43. extern Boolean        gInBackground;
  44.  
  45.  
  46. //----------------------------------------------------------------------
  47. //
  48. //    InstallScrollBars -
  49. //                 
  50. //
  51. //----------------------------------------------------------------------
  52.  
  53. void InstallScrollBars(WindowRef window, DocHnd doc)
  54. {
  55.     Rect        bounds;
  56.     
  57.     
  58.     // calc horiz scroll bar
  59.     bounds = window->portRect;
  60.     
  61.     bounds.right++;
  62.     bounds.left = bounds.right - (kScrollWidth + 1);
  63.     bounds.top--;
  64.     bounds.bottom -= (kScrollWidth - 1);
  65.     
  66.     (**doc).hScroll = NewControl(window, &bounds, nil, true,
  67.                                   0, 0, 0, scrollBarProc, nil);
  68.                                   
  69.     // calc vert scroll bar
  70.     bounds = window->portRect;
  71.  
  72.     bounds.left--;
  73.     bounds.right -= (kScrollWidth - 1);
  74.     bounds.top = bounds.bottom - kScrollWidth;
  75.     bounds.bottom++;
  76.     
  77.  
  78.     (**doc).vScroll = NewControl(window, &bounds, nil, true,
  79.                                   0, 0, 0, scrollBarProc, nil);
  80.  
  81.                                   
  82. }
  83.  
  84.  
  85. //----------------------------------------------------------------------
  86. //
  87. //    AdjustScrollValues -
  88. //                 
  89. //
  90. //----------------------------------------------------------------------
  91.  
  92. void AdjustScrollValues(WindowRef window)
  93. {
  94.     Rect                    contRect;
  95.     Rect                    picFrame;
  96.     ControlHandle            hCntrl;
  97.     ControlHandle            vCntrl;
  98.     short                    oldValue;
  99.     short                    lines;
  100.     short                    max;
  101.     DocHnd                    doc;
  102.     
  103.     doc = (DocHnd)GetWRefCon(window);
  104.     
  105.     if (doc != nil) {
  106.         hCntrl = (**doc).hScroll;
  107.         vCntrl = (**doc).vScroll;
  108.  
  109.         GetContRect(window,&contRect);
  110.         
  111.         if ((**doc).pict != nil || (**doc).world != nil)
  112.             picFrame = (**doc).world->portRect;
  113.         else
  114.             SetRect(&picFrame, 0, 0, 0, 0);
  115.             
  116.         oldValue = GetCtlValue(vCntrl);
  117.         lines = picFrame.bottom - picFrame.top;
  118.         max = lines - contRect.bottom ;
  119.         if ( max < 0 ) max = 0;
  120.         SetCtlMax(vCntrl, max);
  121.     
  122.         if(oldValue + contRect.bottom > lines)
  123.             SetCtlValue(vCntrl, max);
  124.         
  125.         oldValue = GetCtlValue(hCntrl);
  126.         lines = picFrame.right - picFrame.left;
  127.         max = lines - contRect.right;
  128.         if ( max < 0 ) max = 0;
  129.         SetCtlMax(hCntrl, max);
  130.     
  131.         if(oldValue + contRect.right > lines)
  132.             SetCtlValue(hCntrl, max);
  133.                 
  134.     }        
  135.             
  136. }
  137.  
  138.  
  139. //----------------------------------------------------------------------
  140. //
  141. //    AdjustScrollbars -
  142. //                 
  143. //
  144. //----------------------------------------------------------------------
  145.  
  146. void AdjustScrollbars(WindowRef window, Boolean resize)
  147. {
  148.     ControlRef        hCtl;
  149.     ControlRef        vCtl;
  150.     Rect            r;
  151.     DocHnd             doc;
  152.  
  153.     
  154.     doc = (DocHnd)GetWRefCon(window);
  155.     
  156.     if (doc != nil && resize) {
  157.         hCtl = (**doc).hScroll;
  158.         vCtl = (**doc).vScroll;
  159.         GetContRect(window, &r);
  160.         
  161.         // resize horizontal
  162.         MoveControl(hCtl, -1, r.bottom);
  163.         SizeControl(hCtl, (r.right - r.left) + 2, kScrollWidth + 1);
  164.         
  165.         // resize vertical
  166.         MoveControl(vCtl, r.right, -1);
  167.         SizeControl(vCtl, kScrollWidth + 1, r.bottom + 2);
  168.     }
  169.     
  170.     // adjust scrollbar values to match
  171.     AdjustScrollValues(window);
  172. }
  173.  
  174.  
  175. //----------------------------------------------------------------------
  176. //
  177. //    ScrollActionProc -
  178. //                 
  179. //
  180. //----------------------------------------------------------------------
  181.  
  182. pascal void ScrollActionProc(ControlRef control, short part)
  183. {
  184.     WindowRef        window;
  185.     short            amount;
  186.     short            value;
  187.     short            max;
  188.     short            hAmount;
  189.     short            vAmount;
  190.     DocHnd            doc;
  191.     
  192.     window = (**control).contrlOwner;
  193.     doc = (DocHnd)GetWRefCon(window);
  194.     
  195.     if (doc != nil) {
  196.         if ( part != 0 ) {                
  197.             window = (*control)->contrlOwner;
  198.             hAmount = vAmount = 0;
  199.             
  200.             value = GetCtlValue(control);    
  201.             max = GetCtlMax(control);        
  202.         
  203.             switch ( part ) {
  204.                 case inUpButton:
  205.                 case inDownButton:        // one line
  206.                         amount = 8;
  207.                         break;
  208.                         
  209.                 case inPageUp:            // one page
  210.                 case inPageDown:
  211.                         amount = 64;
  212.                         break;
  213.             }
  214.             
  215.             if ( (part == inDownButton) || (part == inPageDown) )
  216.                 amount = -amount;        // reverse direction for a downer
  217.             
  218.             amount = value - amount;
  219.             if ( amount < 0 )
  220.                 amount = 0;
  221.             else if ( amount > max )
  222.                 amount = max; 
  223.             SetCtlValue(control, amount);
  224.             
  225.             amount = value - amount;        // calculate the real change
  226.             
  227.             if ( amount != 0 ) {
  228.                 if (control == (**doc).hScroll)
  229.                     hAmount = amount;
  230.                 else
  231.                     vAmount = amount ;
  232.                         
  233.                 MyScrollPicture(window,hAmount, vAmount);
  234.             }
  235.         }    
  236.     }    
  237. }
  238.     
  239.  
  240. //----------------------------------------------------------------------
  241. //
  242. //    MyScrollPicture -
  243. //                 
  244. //
  245. //----------------------------------------------------------------------
  246.  
  247. void MyScrollPicture(WindowRef window, short hs, short vs)
  248. {
  249.     RgnHandle        updateRgn;
  250.     GWorldPtr        oldWorld;
  251.     GWorldPtr        theWorld;
  252.     GDHandle        oldGD;
  253.     PixMapHandle    thePix;
  254.     Rect            contRect;
  255.     Rect            pictRect;
  256.     DocHnd            doc;
  257.     
  258.     doc = (DocHnd)GetWRefCon(window);
  259.     
  260.     if (doc != nil) {
  261.         GetGWorld(&oldWorld, &oldGD);
  262.         SetPort(window);
  263.         
  264.         GetContRect(window, &contRect);
  265.         ScrollRect(&contRect, hs, vs, updateRgn = NewRgn());
  266.         
  267.         theWorld = (**doc).world;
  268.         pictRect = theWorld->portRect;
  269.         
  270.         thePix = GetGWorldPixMap(theWorld);
  271.         if (LockPixels(thePix)) {
  272.             SetGWorld((CGrafPtr)window, nil);
  273.             OffsetRect(&pictRect, -GetCtlValue((**doc).hScroll), 
  274.                       -GetCtlValue((**doc).vScroll));
  275.  
  276.             CopyBits((BitMap *) *thePix, &window->portBits,
  277.                     &theWorld->portRect, &pictRect, srcCopy,updateRgn);
  278.             
  279.             UnlockPixels(thePix);
  280.             
  281.             SetGWorld(oldWorld, oldGD);
  282.         }            
  283.         ClipRect(&window->portRect);
  284.         DisposeRgn(updateRgn);
  285.     }
  286.         
  287. }    
  288.  
  289.  
  290. //----------------------------------------------------------------------
  291. //
  292. //    GetContRect -
  293. //                 
  294. //
  295. //----------------------------------------------------------------------
  296.  
  297. void GetContRect(WindowRef window,Rect *contRect)
  298. {
  299.     *contRect = window->portRect;    // get the content rect for our window
  300.     
  301.     contRect->right -= 15;            // subtract the scroll bars from the rect
  302.     contRect->bottom -= 15;
  303.  
  304.  
  305. }
  306.